#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace hc
{
	template <class T>
	class vector
	{
		typedef T* iterator;
		typedef const T* const_iterator;
	public:
		vector()
		{}
		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v[i];
			}
			_finish = _start + v.size();
			_end_of_storage = _start + v.capacity();
		}
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		void resize(size_t n,T val=T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				if (n > capacity())
				{
					reserve(n);
				}
				while (_finish != _start + n)
				{
					*_finish = val;
						++_finish;
				}
			}
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}
		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}
		iterator begin()
		{
			return _start;
		}
		const_iterator begin()const
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator end()const
		{
			return _finish;
		}
		void pop_back()
		{
			assert(!empty());
			_finish--;
		}
		bool empty()
		{
			return _start == _finish;
		}
		size_t size()
		{
			return _finish - _start;
		}
		size_t size()const
		{
			return _finish - _start;
		}
		size_t capacity()
		{
			return _end_of_storage - _start;
		}
		size_t capacity()const
		{
			return _end_of_storage - _start;
		}
		iterator insert(iterator pos, const T& val)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				size_t len= pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			iterator start = pos + 1;
			while (start !=_finish)
			{
				*(start - 1) = *start;
				start++;
			}
			_finish--;
			return pos;
		}
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		const T& operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}
		vector<T>& operator=(const vector<T>& v)
		{
			if (this != &v)
			{
				_start = new T[v.capacity];
				for (size_t i = 0; i < v.size(); i++)
				{
					_start[i] = v[i];
				}
				_finish = _start + v.size();
				_end_of_storage = _start + v.capacity();
			}
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;

	};
	void test()
	{
		vector<int> v(10,200);
		/*v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);*/
		std::string s("hello world");
		vector<int> v1(s.begin(), s.end());
		cout << v1.size() << " " << v1.capacity() << endl;
		v1.resize(20, 1);
		cout << v1.size() << " " << v1.capacity() << endl;
		for (auto s : v1)
		{
			cout << s << " ";
		}
		cout << endl;
		v1.pop_back();
		for (auto s : v1)
		{
			cout << s << " ";
		}
		cout << endl;
		v1.insert(v1.begin(), 1000);
		for (auto s : v1)
		{
			cout << s << " ";
		}
		cout << endl;
		v1.erase(v1.begin() + 1);
		for (auto s : v1)
		{
			cout << s << " ";
		}
		cout << endl;
	}
}
